import dayjs from 'dayjs';
import uploadFile from '../../../utils/ossutil/uploadFile';
getApp().authPage.basePage({
  data: {
    formData: {
      "identityTypeValue": "",
      "identityTypeLabel": "",
      "identityCompanyName": "",
      "identityRealName":"",
    },
    showFileList1: true,
    showFileList2: true,
    showFileList3: false,
    showFileList4: false,
    fileList1: [], //身份证
    fileList2: [], //营业执照
    fileList3: [], //持股证明
    fileList4: [], //工牌
    projectUserType: [], //身份字典
    projectUserTypeSelectItem: {
      value: "",
      label: "",
    }, //选中的身份信息
    showIdentity: false, //身份字典弹窗
    maxLengths: 0,
  },
  onLoad() {
    this.getDictType();
  },
  onShow() {
    this.getCurrentUser();
  },
  /**
   * 获取线上用户信息 
   */
  async getCurrentUser() {
    let res = await getApp().http.currentUser();
    try {
      if (res.errorCode === 200) {
        this.setData({
          showFileList1: res.data.userCheckStatus != 1,
        });
      }
    } catch (err) {
      console.log(err);
    }
  },
  showMsgToas(msg) {
    wx.showToast({
      title: msg,
      icon: "none",
    })
  },
  // 提交身份认证 
  async submitFormData() {
    if (this.data.formData.identityTypeValue === "") {
      this.showMsgToas("请选择认证身份");
      return;
    }
    if (this.data.formData.identityTypeValue != 6) {
      if (this.data.formData.identityCompanyName === "") {
        this.showMsgToas("请输入公司名称");
        return;
      }
    }
    if (this.data.formData.identityRealName === "") {
      this.showMsgToas("请输入真实姓名");
      return;
    }  
    if (this.data.showFileList1) {
      if (this.data.fileList1.length === 0) {
        this.showMsgToas("请上传身份证照片");
        return;
      }
    }
    if (this.data.showFileList2) {
      if (this.data.fileList2.length === 0) {
        this.showMsgToas("请选择营业执照照片");
        return;
      }
    }
    if (this.data.showFileList3) {
      if (this.data.fileList3.length === 0) {
        this.showMsgToas("请上传持股证明");
        return;
      }
    }
    if (this.data.showFileList4) {
      if (this.data.fileList4.length === 0) {
        this.showMsgToas("请上传工牌照片");
        return;
      }
    }
    let res = await getApp().http.uploadUserIdentity(this.data.formData);
    if (res.errorCode === 200) {
      this.upLoadImage(res.data.identityId);
    } else {
      this.showMsgToas("请求失败");
    }
  },
  /**
   * 上传信息
   * 
   */
  async uploadFileAliyun(objType, objTypeStr, file) {
    var that = this;
    let {
      fileList1,
      fileList2,
      fileList3,
      fileList4,
    } = this.data;
    let maxLength = fileList1.length + fileList2.length + fileList3.length + fileList4.length;
    let fileImageType = getApp().utils.getFileSuffix(file.url, ".");
    let timeFiles = dayjs(new Date()).format("YYYY/MM/DD");
    let fileName = `${objTypeStr}/${timeFiles}/${new Date().getTime()}.${fileImageType}`
    await uploadFile.uploadFile({
      fileName: fileName,
      filePath: file.url,
      successc: async (res) => {
        this.setData({
          maxLengths: that.data.maxLengths + 1,
        })
        await getApp().http.attachmentUpload({
          "filePath": fileName,
          "objType": objType,
          "objId": file.id,
          "fileSuffix": fileImageType,
        });
        setTimeout(() => {
          if (maxLength == that.data.maxLengths) {
            wx.showModal({
              title: '成功',
              content: '认证信息上传成功',
              showCancel: false,
              success() {
                wx.navigateBack(); //返回上一级
              }
            })
          }
        }, 400)
      }
    })
  },
  // 上传图片
  async upLoadImage(identityId) {
    let {
      fileList1,
      fileList2,
      fileList3,
      fileList4,
      showFileList3,
      showFileList4,
    } = this.data;
    // 身份证
    fileList1.forEach(item => {
      this.uploadFileAliyun(7, 'wechatUserIdcardPortrait', {
        url: item.url,
        id: identityId
      });
    })
    // 营业执照
    fileList2.forEach(item => {
      this.uploadFileAliyun(9, 'wechatUserBusinessLicense', {
        url: item.url,
        id: identityId
      });
    })
    // 持股证明
    if (showFileList3) {
      fileList3.forEach(item => {
        this.uploadFileAliyun(10, 'wechatUserStockProve', {
          url: item.url,
          id: identityId
        });
      })
    }
    // 工牌
    if (showFileList4) {
      fileList4.forEach(item => {
        this.uploadFileAliyun(8, 'wechatUserWorkCard', {
          url: item.url,
          id: identityId
        });
      })
    }
  },
  // 确认
  onConfigPopupItem(e) {
    let {
      key
    } = e.currentTarget.dataset;
    var {
      value,
      label
    } = this.data.projectUserTypeSelectItem;
    console.log(value);
    // 1 身份证 2 营业执照 3 持股证明 4 工牌
    if (value === 0) {
      this.setData({
        showFileList2: true,
        showFileList3: false,
        showFileList4: false,
      })
    }
    if (value === 1) {
      this.setData({
        showFileList2: false,
        showFileList3: true,
        showFileList4: false,
      })
    }
    if (value > 1) {
      this.setData({
        showFileList2: false,
        showFileList3: false,
        showFileList4: true,
      })
    }
    if (value == 6) {
      this.setData({
        showFileList2: false,
        showFileList3: false,
        showFileList4: false,
      })
    }
    if (key === 'standing') {
      this.setData({
        'formData.identityTypeValue': value,
        'formData.identityTypeLabel': label,
      })
    }
    this.closePopup('showIdentity');
  },
  closePopup(e) {
    var key = "";
    if (!key) {
      key = 'showIdentity';
    }
    this.setData({
      [key]: !this.data[key],
    })
  },
  onClickPopupItem(e) {
    var {
      item
    } = getApp().utils.getParameter(e);
    this.setData({
      projectUserTypeSelectItem: item
    })
  },
  setKeyValue(e) {
    var key = e.currentTarget.id;
    this.setData({
      [key]: !this.data[key],
    })
  },
  async getDictType() {
    let res = await getApp().http.dictTypeList();
    if (res.errorCode === 200) {
      this.setData({
        projectUserType: res.data.project_user_type,
      })
    } else {
      console.log("请求失败", res);
    }
  },
  afterReadFileList1(e) {
    var file = e.detail.file;
    file.upload = false;
    this.data.fileList1.push(file);
    this.setData({
      fileList1: this.data.fileList1,
    });
  },
  afterReadFileList2(e) {
    var file = e.detail.file;
    file.upload = false;
    this.data.fileList2.push(file);
    this.setData({
      fileList2: this.data.fileList2,
    });
  },
  afterReadFileList3(e) {
    var file = e.detail.file;
    file.upload = false;
    this.data.fileList3.push(file);
    this.setData({
      fileList3: this.data.fileList1,
    });
  },
  afterReadFileList4(e) {
    var file = e.detail.file;
    file.upload = false;
    this.data.fileList4.push(file);
    this.setData({
      fileList4: this.data.fileList4,
    });
  },
  delFileList1(e) {
    let imageIndex = e.detail.index;
    var temp_filter = this.data.fileList1;
    this.setData({
      fileList1: temp_filter.filter((item, index) => index !== imageIndex),
    });
  },
  delFileList2(e) {
    let imageIndex = e.detail.index;
    var temp_filter = this.data.fileList2;
    this.setData({
      fileList2: temp_filter.filter((item, index) => index !== imageIndex),
    });
  },
  delFileList3(e) {
    let imageIndex = e.detail.index;
    var temp_filter = this.data.fileList3;
    this.setData({
      fileList3: temp_filter.filter((item, index) => index !== imageIndex),
    });
  },
  delFileList4(e) {
    let imageIndex = e.detail.index;
    var temp_filter = this.data.fileList4;
    this.setData({
      fileList4: temp_filter.filter((item, index) => index !== imageIndex),
    });
  },
  setInput(e) {
    let key = e.currentTarget.id;
    if (key) {
      this.setData({
        ["formData." + key]: e.detail,
      });
    }
  },
});